// pages/cooperation/houseStatusUpdate/houseStatusUpdate.js

import api from "../../../common/api";

const app = getApp();
Page({
  options: {
    pureDataPattern: /^_/,
  },

  /**
   * 页面的初始数据
   */
  data: {
    completedUrl: app.globalData.cdnResourceUrl + "status_update_2.png",
    soldOutVisble: false,
    resultPopVisble: false,
    animationData: {},
    isAnimation: false, //处于动画中
    uncertainList: [], //需要进一步确认的房源
    _houseList: [], // 待处理房源
    houseItem: {}, //当前处理中的房源
    houseItemNext: {}, //下一个房源
    total: 0, //房源总数
    loading: false,
    isFirst: true, //首次加载
    cooperationIndex:false, //合作房源首页轮播进入的特殊操作
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.showLoading(true)
    getApp().globalData.loginPromise.then(() => {
      if(options.cooperationIndex&&options.cooperationIndex=='index'){ //判断是不是在合作房源首页轮播进入的
        this.setData({
          cooperationIndex:true
        })
      }
      api.queryCooperationConfirm(
        { page: 1, pageSize: 100 },
        ({ data }) => {
          this.setData({ isFirst: false })
          this.hideLoading(true);
          if (data.content && data.content.rows) {
            const { rows } = data.content;
            this.setData({ total: rows.length });
            this.renderHouseInfo(rows);
          }
        },
        () => {
          this.hideLoading(true)
          this.setData({ isFirst: false })
        }
      );
    });
  },

  renderHouseInfo(rows) {
    if (!rows || !rows.length) return;
    this.data._houseList = rows;
    const i = 0;
    this.setData({
      houseItem: rows[i],
      houseItemNext: rows[i + 1] || rows[i],
    });
  },

  handleYes() {
    if (this.data.isAnimation || this.data.loading) return;

    // 测试
    // return this.renderNext()

    this.showLoading();
    const { confirmId, houseCooperationId } = this.data.houseItem;
    api.cooperationConfirm(
      {
        status: "1",
        confirmId,
        houseCooperationId,
      },
      ({ data }) => {
        this.hideLoading();
        this.renderNext();
      },
      this.hideLoading.bind(this)
    );
  },
  handleUncertain() {
    if (this.data.isAnimation || this.data.loading) return;

    const { confirmId, houseCooperationId } = this.data.houseItem;
    const item = this.data.houseItem;

    this.showLoading();
    api.cooperationConfirm(
      { status: "2", confirmId, houseCooperationId },
      ({ data }) => {
        this.hideLoading();
        if (this.data.uncertainList.indexOf(item) == -1) {
          this.setData({ uncertainList: this.data.uncertainList.concat(item) });
        }
        this.renderNext();
      },
      this.hideLoading.bind(this)
    );
  },
  handleNo() {
    if(this.data.cooperationIndex){
      const cooperationId = this.data.houseItem.houseCooperationId;
      api.downMyCooperation({ cooperationId},({ data }) => {
        this.hideLoading();
        this.renderNext();
      },
      this.hideLoading.bind(this)
      );
    } else{
      if (this.data.isAnimation || this.data.loading) return;
      this.setData({ soldOutVisble: true });
    }
  },

  // 选择下架原因
  onSoldHide(e) {
    const { label, isConfirm } = e.detail;
    this.setData({ soldOutVisble: false });

    if (isConfirm) {
      this.showLoading();
      const cooperationId = this.data.houseItem.houseCooperationId;
      api.downMyCooperation(
        { cooperationId, downReason: label },
        ({ data }) => {
          this.hideLoading();
          this.renderNext();
        },
        this.hideLoading.bind(this)
      );
    }
  },

  /**
   * 渲染流程
   * 首先 上下两张卡片
   * 当前卡片向右滑动，动画完成之后，把当前卡片渲染成下一张卡片，此时两张卡片内容一样，
   * 延迟100ms 把滑动出屏幕的卡片渲染成 
   * 把当前卡片位置还原回来，此时当前卡片依然盖着下一张卡片，下边卡片继续渲染成下一张卡片内容
   */
  renderNext() {
    const rows = this.data._houseList.slice(1);
    if (rows.length) {
      this.data._houseList = rows;

      this.data.isAnimation = true;
      this.selectComponent("#com-card-template").start(() => {
        const rows = this.data._houseList;
        this.setData({
          houseItem: rows[0],
        });
      });
    } else {
      // 如果还有继续处理的则预先渲染一下卡片
      if (this.data.uncertainList.length) {
        const { confirmId } = this.data.houseItem;
        const rows = this.data.uncertainList;
        const i = rows.findIndex((v) => v.confirmId == confirmId);
        if (i > -1) {
          rows.unshift(rows.splice(i, 1)[0]);
        }
        setTimeout(() => {
          this.renderHouseInfo(rows);
        }, 800);
      }

      this.setData({ resultPopVisble: true });
    }
  },

  // 动画结束
  handleAniEnd() {
    const rows = this.data._houseList;
    this.data.isAnimation = false;
    this.setData({
      houseItemNext: rows[1] || rows[0],
    });
  },

  onResultHide() {
    this.setData({
      resultPopVisble: false,
      uncertainList: [],
    });
  },

  onResultLeave() {
    if (getCurrentPages().length > 1) {
      wx.navigateBack();
    } else {
      wx.reLaunch({ url: "/pages/index/index" });
    }
  },

  showLoading(show) {
    show && wx.showLoading()
    this.data.loading = true;
  },
  hideLoading(hide) {
    hide && wx.hideLoading()
    this.data.loading = false;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
});
